package com.example.image

import android.content.Context
import android.net.Uri
import android.widget.ImageView
import com.example.image.loder.FrescoImageLoader
import com.example.image.loder.GlideImgLoader
import com.example.image.loder.NativeImageLoader
import com.example.image.loder.PicassoImageLoader
import com.example.image.option.ImageOptions
import com.example.image.skill.ImageLoader
import com.example.image.strategy.ImageStrategy
import java.io.File


/**
 * 支持多种加载策略的图片加载器
 *
 * 备注 ImgLoader,Builder.setImageStrategy(ImageStrategy.GLIDE),.build().loadFileIntoImageView
 * */
class ImgLoader private constructor(private var strategy : ImageStrategy= ImageStrategy.GLIDE): ImageLoader {

    private var imgLoaderStrategy =when(strategy){
        ImageStrategy.GLIDE->GlideImgLoader()
        ImageStrategy.FRESCO->FrescoImageLoader()
        ImageStrategy.PICASSO->PicassoImageLoader()
        ImageStrategy.NATIVE->NativeImageLoader()
    }

    companion object Builder{

        private  var strategy : ImageStrategy =ImageStrategy.GLIDE


        fun setImageStrategy(strategy: ImageStrategy){
            this.strategy=strategy
        }

        //返回ImgLoader
        fun build():ImgLoader{
            return ImgLoader(strategy)
        }
    }


    override fun loadFileIntoImageView(
        context: Context,
        file: File,
        target: ImageView,
        options: ImageOptions
    ) {
        //要拿到一个具体的图片加载器
        imgLoaderStrategy.loadFileIntoImageView(context, file, target, options)
    }

    override fun loadResIntoImageView(
        context: Context,
        resId: Int,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadResIntoImageView(context, resId, target, options)
    }

    override fun loadUrlIntoImageView(
        context: Context,
        uri: Uri,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadUrlIntoImageView(context, uri, target, options)
    }

    override fun loadNetIntoImageView(
        context: Context,
        url: String,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadNetIntoImageView(context, url, target, options)
    }

    override fun loadBytesIntoImageView(
        context: Context,
        bytes: ByteArray,
        target: ImageView,
        options: ImageOptions
    ) {
        imgLoaderStrategy.loadBytesIntoImageView(context, bytes, target, options)
    }
}